(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("setlDetailStyleService", setlDetailStyleService);
	setlDetailStyleService.$inject = [ "$injector", "clsConstants", "$filter", "arithService" ];
	function setlDetailStyleService($injector, clsConstants, $filter, arithService) {
		var settlementInitFactory = function() {
			return $injector.get("settlementInitFactory");
		};
		
		function payCcyStyle(setlDetail) {
			if ((setlDetail.childFlag == "N" || setlDetail.childFlag == "Y" && setlDetail.itemType == "P") && !isRolloverRemittance(setlDetail.itemSubtype)) {
				setlDetail.payCcy_Readonly = false;
			} else {
				setlDetail.payCcy_Readonly = true;
			}
		}

		function fxRateStyle(setlDetail) {
			if (setlDetail.itemType != "P" && setlDetail.childFlag == "Y") {
				setlDetail.fxRate_Readonly = true;
				setlDetail.fxRate2_Readonly = true;
				setlDetail.fxRateType_Readonly = true;
				setlDetail.fxRateType2_Readonly = true;
			}
			// else{
			// setlDetail.fxRate_Readonly = false;
			// setlDetail.fxRate2_Readonly = false;
			// setlDetail.fxRateType_Readonly = false;
			// setlDetail.fxRateType2_Readonly = false;
			// }
		}

		function allocCodeStyle(setlDetail) {
			if (setlDetail.itemType != "P" && setlDetail.childFlag == "N" && !isRolloverRemittance(setlDetail.itemSubtype)) {
				setlDetail.allocCode_Readonly = false;
			} else {
				setlDetail.allocCode_Readonly = true;
			}
			setlDetail.allocCode_Show = (setlDetail.itemType != "P");
		}

		function paydetailSeqStyle(setlDetail) {
			if (setlDetail.itemHoldFlag == "Y" || setlDetail.uItemWaive == "Y") {
				setlDetail.paydetailSeq_Readonly = true;
			}
			if (setlDetail.itemType == "F" && isRolloverRemittance(setlDetail.itemSubtype)) {
				setlDetail.paydetailSeq_Readonly = true;
			} else {
				setlDetail.paydetailSeq_Readonly = false;
			}
		}

		function fxCrossStyle(setlDetail) {
			var translate = $filter("translate");
			setlDetail.fxCrossRateNote = "(" + translate("label.settlement.hinttext.from") + " " + setlDetail.itemCcy + " " + translate("label.settlement.hinttext.to") + " "
					+ setlDetail.payCcy + ")";
			setlDetail.fxContractNo_Show = setlDetail.crossFxRateSpan_Show;
			setlDetail.fxCrossRate_Show = setlDetail.crossFxRateSpan_Show;
			setlDetail.fxCrossRateCreateNew_Show = setlDetail.crossFxRateSpan_Show;
			setlDetail.fxCrossRateNote_Show = setlDetail.crossFxRateSpan_Show;

			setlDetail.fxRateType_Show = setlDetail.fxRateSpan_Show;
			setlDetail.fxRate_Show = setlDetail.fxRateSpan_Show;
			setlDetail.fxRate1CcyFrom_Show = setlDetail.fxRateSpan_Show;
			setlDetail.fxRateType2_Show = setlDetail.fxRateSpan_Show;
			setlDetail.fxRate2_Show = setlDetail.fxRateSpan_Show;
			setlDetail.fxRate2CcyFrom_Show = setlDetail.fxRateSpan_Show;
		}

		function taxAmtStyle(setlDetail) {
			if (setlDetail.taxAmt && !arithService.eq(setlDetail.taxAmt, 0)) {
				setlDetail.taxAmt_Show = true;
			} else {
				setlDetail.taxAmt_Show = false;
			}
		}

		function taxWaiveFlagStyle(setlDetail) {
			if (setlDetail.taxAmt && !arithService.eq(setlDetail.taxAmt, 0)) {
				setlDetail.taxWaiveFlag_Show = true;
			} else {
				setlDetail.taxWaiveFlag_Show = false;
			}
		}

		function itemHoldFlagStyle(settlement, setlDetail) {
			if (setlDetail.itemType == "F" && setlDetail.childFlag == "N") {
				if (!("Y" == setlDetail.feeNewFlag && (setlDetail.feeId != null && setlDetail.feeId != ""))) {
					setlDetail.itemHoldFlag_Show = true;
				}
			}
			// if it repay , interest item can be holded.
			settlementInitFactory().getFunction(settlement.settlementInfo, "dynamicConfigItemHoldFlag")(settlement, setlDetail);
			if (setlDetail.uItemWaive == "Y") {
				if (setlDetail.itemWaiveFlag == "Y") {
					setlDetail.itemHoldFlag_Readonly = true;
				}
			}
		}

		function itemWaiveFlagStyle(settlement, setlDetail) {
			if (setlDetail.itemType == "F" && setlDetail.childFlag == "N") {
				if (!("Y" == setlDetail.feeNewFlag && (setlDetail.feeId != null && setlDetail.feeId != ""))) {
					if (setlDetail.uItemWaive == "Y") {
						setlDetail.itemWaiveFlag_Show = true;
					}
				}
			}
			settlementInitFactory().getFunction(settlement.settlementInfo, "dynamicConfigItemWaiveFlag")(settlement, setlDetail);
			if (setlDetail.itemHoldFlag == "Y") {
				setlDetail.itemWaiveFlag_Readonly = true;
			}
		}

		function itemTransferFlagStyle(settlement, setlDetail) {
			setlDetail.itemTransferFlag_Show = false;
			// loan/ custpay/dcharge
			if (setlDetail.itemType == "I" && setlDetail.childFlag == "N") {
				if (setlDetail.uItemTransfer == "Y") {
					setlDetail.itemTransferFlag_Show = true;
				}
			}
			// repayment
			settlementInitFactory().getFunction(settlement.settlementInfo, "dynamicConfigItemTransferFlag")(settlement, setlDetail);
			if (setlDetail.itemHoldFlag == "Y") {
				setlDetail.itemTransferFlag_Readonly = true;
			}
			if (setlDetail.uItemWaive == "Y") {
				if (setlDetail.itemWaiveFlag == "Y") {
					setlDetail.itemTransferFlag_Readonly = true;
				}
			}
		}

		function itemCarryFlagStyle(settlement, setlDetail) {
			if (setlDetail.itemHoldFlag == "Y") {
				setlDetail.itemCarryFlag_Readonly = true;
			}
			if (setlDetail.uItemWaive == "Y") {
				if (setlDetail.itemWaiveFlag == "Y") {
					setlDetail.itemCarryFlag_Readonly = true;
				}
			}
			settlementInitFactory().getFunction(settlement.settlementInfo, "dynamicConfigItemCarryFlag")(settlement, setlDetail);
		}

		function processTaxWaiveLabel(setlDetail) {
			if (setlDetail.taxWaiveFlag == "Y") {
				setlDetail.taxAmt_label = "label.settlement.init.taxwaive";
			} else {
				setlDetail.taxAmt_label = "label.settlement.init.tax";
			}
			if (setlDetail.setlDetailConfig) {
				setlDetail.setlDetailConfig.gridConfigs.taxAmt.label = setlDetail.taxAmt_label;
			}
		}

		/**
		 * disable fields: can't accept input and clear values, and clean up all
		 * labels, hidden icons
		 * 
		 * @param itemNo
		 */
		function disableFxRateAndFxRate2(setlDetail) {
			setlDetail.fxRateType = "";
			setlDetail.fxRate = "";
			setlDetail.fxRateType2 = "";
			setlDetail.fxRate2 = "";
			setlDetail.fxRate1CcyFrom = "";
			setlDetail.fxRate2CcyFrom = "";
			setlDetail.fxRateSpan_Show = false;
		}

		function initSetlDetailStyle(settlement, setlDetail) {
			payCcyStyle(setlDetail);
			fxRateStyle(setlDetail);
			allocCodeStyle(setlDetail);
			paydetailSeqStyle(setlDetail);
			fxCrossStyle(setlDetail);
			taxAmtStyle(setlDetail);
			taxWaiveFlagStyle(setlDetail);
			itemHoldFlagStyle(settlement, setlDetail);
			itemWaiveFlagStyle(settlement, setlDetail);
			itemTransferFlagStyle(settlement, setlDetail);
			itemCarryFlagStyle(settlement, setlDetail);
		}
		
		function isRolloverRemittance(itemSubtype) {
			if (itemSubtype == clsConstants.SetlDetailItemSubType.ROLLOVER || itemSubtype == clsConstants.SetlDetailItemSubType.ROLLOVER_PASTDUE_INTEREST
					|| itemSubtype == clsConstants.SetlDetailItemSubType.ROLLOVER_PD_PENALTY_INT || itemSubtype == clsConstants.SetlDetailItemSubType.ROLLOVER_IR_PD_INT
					|| itemSubtype == clsConstants.SetlDetailItemSubType.ROLLOVER_IR_PD_PENALTY_INT) {
				return true;
			} else {
				return false;
			}
		}

		return {
			disableFxRateAndFxRate2 : disableFxRateAndFxRate2,
			fxCrossStyle : fxCrossStyle,
			initSetlDetailStyle : initSetlDetailStyle,
			processTaxWaiveLabel : processTaxWaiveLabel
		};

	}
})(angular);
